﻿using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Web;

namespace ASKM.Rachao.Web.Site.RachaoMvcHelper
{

    public static class RachaoMvcHelperExtensions
    {

        /// <summary>
        /// Renders Validation Summary content through pop-up
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="excludePropertyErrors">Exclude properties errors</param>
        /// <param name="title">Popup title</param>
        /// <param name="message">Popup message</param>
        /// <returns>IHtmlString</returns>
        public static IHtmlString ValidationSummaryPopUp(this HtmlHelper helper, bool excludePropertyErrors,string title, string message)
        {
            StringBuilder sb = new StringBuilder();

            var validationSummaryMessage = helper.ValidationSummary(excludePropertyErrors, message);
            sb.Append(ShowDivDialog("validationSummary", title, validationSummaryMessage!=null?validationSummaryMessage.ToHtmlString():String.Empty));            
            
            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// Renders a dialog box
        /// </summary>
        /// <param name="dialogBoxName">Dialog box name</param>
        /// <param name="title">Title of dialog box</param>
        /// <param name="message">Message for dialog box</param>
        /// <returns>Dialog box string</returns>
        public static string RenderDialogBox(string dialogBoxName, string title, string message)
        {
            return String.Format("<div id='{0}dialogBox' Title='{1}'>{2}</div>", dialogBoxName, title, message);
        }


        /// <summary>
        /// Shows a div diaglo if message exists
        /// </summary>
        /// <param name="dialogBoxName">Dialog box name</param>
        /// <param name="title">dialog box title</param>
        /// <param name="message">Dialog box message</param>
        /// <param name="width">Dialog box width</param>
        /// <param name="height">Dialog box height</param>
        /// <param name="isModal">Is dialog box modal?</param>
        /// <returns>Dialog box</returns>
        public static string ShowDivDialog(string dialogBoxName, string title, string message, int? width = 400, int? height = null, bool? isModal = true)
        {
            if (!String.IsNullOrEmpty(message) || !String.IsNullOrWhiteSpace(message))
            {
                StringBuilder sbDialogBoxOptions = new StringBuilder();
                sbDialogBoxOptions.AppendFormat("show:'blind',");
                sbDialogBoxOptions.AppendFormat("hide: 'explode',");                
                if(width.HasValue)
                    sbDialogBoxOptions.AppendFormat("width:{0},",width.Value);

                if (height.HasValue)
                    sbDialogBoxOptions.AppendFormat("height:{0},", height.Value);

                if(isModal.HasValue)
                    sbDialogBoxOptions.AppendFormat("modal: {0},",isModal.Value.ToString().ToLower());

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(@"<script type='text/javascript' />");
                sb.AppendFormat(@"    $(document).ready(function(){{
                                    
                                    $('#{0}dialogBox').dialog({{                                                                                                                                                                            
                                        
                                        {1}
                                        autoOpen:false
    
                                    }});

                                    
                                    $('#{0}dialogBox').dialog('open');                                    
    
                                }})", dialogBoxName, sbDialogBoxOptions.ToString());

                sb.AppendLine(@"</script>");

                sb.AppendLine(RenderDialogBox(dialogBoxName, title, message));

                return sb.ToString();
            }
            return String.Empty;
        }

    }

}